/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.xeerkat.xmpp;

import java.io.Writer;
import java.util.logging.Logger;
import org.infoset.xml.ItemDestination;

/**
 *
 * @author alex
 */
public class OrderedStanzaWriter extends StanzaWriter {

   int requests = 0;
   public OrderedStanzaWriter(Logger log,Writer out)
   {
      super(log,out);
   }
   
   public ItemDestination next() {
      synchronized (this) {
         boolean needWait = requests>0;
         requests++;
         if (needWait) {
            try {
               wait();
            } catch (InterruptedException ex) {
            }
         }
      }
      return super.next();
   }
   
   public void release() {
      synchronized (this) {
         requests--;
         notify();
      }
   }
   
   public void terminate() {
      super.terminate();
      synchronized (this) {
         notifyAll();
      }
   }
}
